<template>
  <a-card class="j-address-list-right-card-box" :loading="cardLoading" :bordered="false">
    <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row :gutter="10">

          <a-col :md="6" :sm="12">
            <a-form-item label="姓名" style="margin-left:8px">
              <a-input placeholder="请输入姓名查询" v-model="queryParam.realname"></a-input>
            </a-form-item>
          </a-col>

          <a-col :md="6" :sm="12">
            <a-form-item label="工号" style="margin-left:8px">
              <a-input placeholder="请输入工号查询" v-model="queryParam.workNo"></a-input>
            </a-form-item>
          </a-col>

           <a-col :md="6" :sm="12">
            <a-form-item label="手机号" style="margin-left:8px">
              <a-input placeholder="请输入手机号查询" v-model="queryParam.phone"></a-input>
            </a-form-item>
          </a-col>

          <a-col v-show="false" :md="6" :sm="12">
            <a-form-item label="部门" style="margin-left:8px">
              <a-input placeholder="请输入部门" v-model="queryParam.orgCode"></a-input>
            </a-form-item>
          </a-col>

          <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
            <a-col :md="6" :sm="24">
             <a-button type="primary" @click="searchQuery" icon="search" style="margin-left: 8px">查询</a-button>
              <a-button @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
              <a-button type="primary" icon="download" @click="handleExportXls('通讯录导出')" style="margin-left: 8px" :loading="exportLoading">导出</a-button>
            </a-col>
          </span>
        </a-row>
      </a-form>
    </div>

    <a-table
      ref="table"
      size="middle"
      bordered
      :components="draggableComponent"
      rowKey="userId"
      :pagination="ipagination"
      :columns="columns"
      :dataSource="dataSource"
      :loading="loading"
      @change="handleTableChange">
    </a-table>

  </a-card>
</template>

<script>
import {getAction,downFile} from '@/api/manage'
import {JeecgListMixin} from '@/mixins/JeecgListMixin'
import { filterMultiDictText, initDictOptions } from '@/components/dict/JDictSelectUtil'

export default {
    name: 'AddressListRight',
    mixins: [JeecgListMixin],
    components: {},
    props: ['value'],
    data() {
      return {
        dictOptions: {},
        description: '用户信息',
        cardLoading: true,
        positionInfo: {},
        columns: [
          {
            title: '序号',
            key: 'rowIndex',
            dataIndex: '',
            width: 30,
            align: 'center',
            customRender: (t, r, i) => parseInt(i) + 1
          },
          {
            title: '姓名',
            width: 60,
            align: 'center',
            dataIndex: 'realname'
          },
          {
            title: '工号',
            width: 100,
            align: 'center',
            dataIndex: 'workNo'
          },
          {
            title: '部门',
            width: 160,
            align: 'center',
            dataIndex: 'departName'
          },
          // {
          //   title: '职务级别',
          //   width: 100,
          //   align: 'center',
          //   dataIndex: 'post',
          //   customRender: (text) => {
          //     if (!text) {
          //       return ''
          //     } else {
          //       return filterMultiDictText(this.dictOptions['post'], text + '')
          //     }
          //   }
          // },
          {
            title: '职务级别及岗位',
            width: 100,
            align: 'center',
            dataIndex: 'levelPostName',
          },
          {
            title: '手机号',
            width: 100,
            align: 'center',
            dataIndex: 'phone'
          },
        ],
        url: {
          list: '/api/erpmng/sys/user/queryByOrgCodeForAddressList',
          listByPosition: '/api/erpmng/sys/position/list',
          exportXlsUrl: '/api/erpmng/sys/user/exportAddressList'
        }
      }
    },
    watch: {
      value: {
        immediate: true,
        handler(orgCode) {
          this.dataSource = []
          this.queryParam.orgCode = orgCode
          this.loadData(1, orgCode)
        }
      },
    },
    created() {
      //加载数据字典信息
      this.initDictConfig();
      //this.queryPositionInfo();
    },
    methods: {
      //加载数据字典信息
      initDictConfig() {

        initDictOptions('erp_org_position_level,level_name,id').then((res) => {
          if (res.success) {
            this.$set(this.dictOptions, 'post', res.result)
          }
        })
        initDictOptions('sys_position,name,id').then((res) => {
          if (res.success) {
            this.$set(this.dictOptions, 'postIds', res.result)
          }
        })
      },
      loadData(pageNum, orgCode) {
        this.loading = true
        if (pageNum === 1) {
            this.ipagination.current = 1
        }
        // update-begin- --- author:wangshuai ------ date:20200102 ---- for:传过来的部门编码为空全查
        if (!orgCode) {
            getAction(this.url.list, {
                ...this.getQueryParams()
            }).then((res) => {
                if (res.success) {
                    this.dataSource = res.result.records
                    this.ipagination.total = res.result.total
                }
            }).finally(() => {
                this.loading = false
                this.cardLoading = false
            })
          // update-end- --- author:wangshuai ------ date:20200102 ---- for:传过来的部门编码为空全查
        }else{
        //加载数据 若传入参数1则加载第一页的内容
        getAction(this.url.list, {
          orgCode,
          ...this.getQueryParams()
        }).then((res) => {
          if (res.success) {
            this.dataSource = res.result.records
            this.ipagination.total = res.result.total
          }
        }).finally(() => {
          this.loading = false
          this.cardLoading = false
        })
        }
      },

      searchQuery() {
        this.loadData(1, this.value)
      },
      searchReset() {
        // this.queryParam = {
        //   orgCode: null
        // }
        // 只清空姓名工号手机号
        this.$delete(this.queryParam,'realname')
        this.$delete(this.queryParam,'workNo')
        this.$delete(this.queryParam,'phone')
        this.loadData(1, this.value)
      },

      handleTableChange(pagination, filters, sorter) {
        if (Object.keys(sorter).length > 0) {
          this.isorter.column = sorter.field
          this.isorter.order = 'ascend' === sorter.order ? 'asc' : 'desc'
        }
        this.ipagination = pagination
        this.loadData(null, this.value)
      },

      // 查询职务信息
      // queryPositionInfo() {
      //   getAction(this.url.listByPosition, { pageSize: 99999 }).then(res => {
      //     if (res.success) {
      //       let positionInfo = {}
      //       res.result.records.forEach(record => {
      //         positionInfo[record['code']] = record['name']
      //       })
      //       this.positionInfo = positionInfo
      //     }
      //   })
      // }
    }
  }
</script>
<style>
  .j-address-list-right-card-box .ant-table-placeholder {
    min-height: 46px;
  }
</style>
<style scoped>
  .j-address-list-right-card-box {
    height: 100%;
    min-height: 300px;
  }
</style>
